package com.hboxs.asl.controller.asl.product;

import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.Product;
import com.hboxs.asl.entity.Specification;
import com.hboxs.asl.service.ProductService;
import com.hboxs.asl.service.SpecificationValueService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Set;

/**
 * Created by xxx on 2016/12/10
 * 规格接口
 */
@Controller(value = "aslSpecificationController")
@RequestMapping(value = "/weChat/spec")
public class SpecController extends BaseController {

    @Resource(name = "productServiceImpl")
    private ProductService productService;
    @Resource(name = "specificationValueServiceImpl")
    private SpecificationValueService specificationValueService;

    /**
     * 请求规格
     *
     * @param pId 商品id
     * @return json
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public
    @ResponseBody
    Object list(Long pId) {

        try {
            Product product = productService.find(pId);
            if (product == null) {
                return JsonMessage.error("找不到该商品");
            }
            Set<Specification> specifications = product.getSpecifications();
            //商品未拥有的规格值
            Set<Long> disableIds = specificationValueService.notPossess(product);

            return JsonMessage.success(

                    "spec_list", JsonMessage.optimiseObj(specifications, "id", "name", "specificationValues.id#name")

                    , "disable_ids", disableIds
            );

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 请求禁用规格
     *
     * @param pId 商品id
     * @return json
     */
    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    public
    @ResponseBody
    Object disable(Long pId, Long[] ids) {
        try {
            Product product = productService.find(pId);
            if (product == null) {
                return JsonMessage.error("找不到该商品");
            }
            if (!specificationValueService.checkExist(ids)) {
                return JsonMessage.error("找不到对应规格值");
            }
            if (!specificationValueService.checkPossess(product, ids)) {
                return JsonMessage.error("商品未拥有所选规格值");
            }
            if (!specificationValueService.checkSame(ids)) {
                return JsonMessage.error("不能传入相同规格的规格值");
            }
            Set<Long> disableIds = specificationValueService.findDisableIds(product, ids);
            return JsonMessage.success(disableIds);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 获取规格商品
     *
     * @param pId     商品Id
     * @param specIds 规格是值Id
     * @return json
     */
    @RequestMapping(value = "/find", method = RequestMethod.GET)
    public
    @ResponseBody
    Object find(Long pId, Long[] specIds) {
        try {
            Product product = productService.find(pId);
            if (product == null) {
                return JsonMessage.error("找不到该商品");
            }
            if (!specificationValueService.checkExist(specIds)) {
                return JsonMessage.error("找不到对应规格值");
            }
            if (!specificationValueService.checkPossess(product, specIds)) {
                return JsonMessage.error("商品未拥有所选规格值");
            }
            if (!specificationValueService.checkSame(specIds)) {
                return JsonMessage.error("不能传入相同规格的规格值");
            }
            Product target = specificationValueService.findProductBySpecVals(product, specIds);

            return JsonMessage.success(JsonMessage.optimiseObj(target, "id", "specificationValues.name"));

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

}
